﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CRM.Models;
using Microsoft.EntityFrameworkCore;
using Z.EntityFramework.Plus;
using Microsoft.AspNetCore.Authorization;
using CRM.Authorization.Rule;
namespace CRM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : BaseController
    {
        private readonly MyDbContext _dbContext;

        public TestController(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        [HttpGet("[action]")]
        [Authorize(Policy = "user")]
        public ResultStruct AuthPolicy()
        {
            return Success();
        }
        [AllowAnonymous]
        [Route("[Action]")]
        public ResultStruct Param(string PassWord, string UserName)
        {
            //{"message":"用于测试直接传参访问","data":{"userName":"1","passWord":"2"},"code":20000}
            return Success(new { UserName = UserName, PassWord = PassWord }, "用于测试直接传参访问");
        }

        [HttpGet("[action]")]
        [Permission(Authorization.Permissions.SocialMarketingTasks.MODULE)]
        public ResultStruct AuthPermission()
        {
            return Success();
        }

        [Permission(Authorization.Permissions.SocialMarketingTasks.INDEX)]
        public ResultStruct Index()
        {
            return Success();
        }
        // GET: api/<TestController>
        [HttpGet]
        public async Task<List<Test>> Get()
        {
            return await _dbContext.Tests.OrderBy(m => m.Id).Take(10).ToListAsync();
        }

        // GET api/<TestController>/5
        [HttpGet("{id:int}")]
        public async Task<ResultStruct> Get(int id)
        {
            var a = new[] {1};
            await _dbContext.Tests.Where(m => a.Contains(m.Id)).UpdateAsync(m =>new (){Str10 = "1222"});
            // await _dbContext.SaveChangesAsync();
            var row = await _dbContext.Tests.Where(m => m.Id == id).FirstAsync();
            return row == null ? Error() : Success(data: row);
        }

        // POST api/<TestController>
        [HttpPost]
        public async Task Post([FromBody] string value)
        {
            var row = await _dbContext.Tests.Where(m => m.Id == 1).FirstAsync();
            if (row == null)
            {
                return;
            }

            row.Str10 = value;
            await _dbContext.SaveChangesAsync();
            // return row;
        }

        // PUT api/<TestController>/5
        [HttpPut("{id:int}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<TestController>/5
        [HttpDelete("{id:int}")]
        public async Task Delete(int id)
        {
            var row = await _dbContext.Tests.Where(m => m.Id == 1).FirstAsync();
            if (row == null)
            {
                return;
            }

            _dbContext.Remove(row);
            await _dbContext.SaveChangesAsync();
        }
    }
}